package dd.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import dd.control.BackToMainScreenController;
import dd.control.ReportRequestController;
import dd.model.Event;

public class AdminScreen extends JFrame {

	private JPanel contentPane;
	private JScrollPane pnlEvents;
	private JButton btnCreateReport;
	private JButton btnBack;
	private JList listEvent;
	private JLabel lblErrorMessage;
	private JLabel lblInfo;
	private JButton btnDeleteEvent;

	// only retrieve the passed event (135) for now...
	public AdminScreen(List<Event> events) {
		prepareAdminScreen();

		lblErrorMessage.setVisible(false);

		Vector<String> eventData = new Vector<String>();
		if (events != null) {
			for (Event event : events) {
				eventData.add(event.getEventID().trim() + ": " + event.getEventName().trim());
			}
		}

		listEvent.setListData(eventData);

		btnCreateReport.addActionListener(new ReportRequestController(this, null));
		btnBack.addActionListener(new BackToMainScreenController(this));

		// btnDeleteEvent.addActionListener(new ActionListener() {
		// public void actionPerformed(ActionEvent e) {
		// int decision = confirmPopupDialog();
		// // yes: 0, no: 1
		// if (decision == 0) {
		//
		// }
		// }
		// });
	}

	private int confirmPopupDialog() {
		return JOptionPane.showOptionDialog(this, "Are you sure to delete this event?", "Confirmation",
				JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);
	}

	/**
	 * @return the selected event string or null if not select
	 */
	public String getSelectetEvent() {
		String selectedEvent = null;
		JList listView = (JList) pnlEvents.getViewport().getView();
		selectedEvent = (String) listView.getSelectedValue();

		return selectedEvent;
	}

	public JLabel getLblErrorMessage() {
		return lblErrorMessage;
	}

	public JLabel getLblInfo() {
		return lblInfo;
	}

	private void prepareAdminScreen() {
		setResizable(false);
		setTitle("Administration Board");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 392, 280);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		getContentPane().setLayout(null);

		pnlEvents = new JScrollPane();
		pnlEvents.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		pnlEvents.setBounds(10, 40, 183, 192);
		getContentPane().add(pnlEvents);

		listEvent = new JList();
		listEvent.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		pnlEvents.setViewportView(listEvent);

		JLabel lblViewEvents = new JLabel("View Events:");
		lblViewEvents.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblViewEvents.setBounds(10, 11, 88, 23);
		getContentPane().add(lblViewEvents);

		btnCreateReport = new JButton("Create Report!");
		btnCreateReport.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnCreateReport.setBounds(240, 40, 135, 40);
		getContentPane().add(btnCreateReport);

		btnBack = new JButton("<< Back");
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnBack.setBounds(296, 200, 80, 32);
		getContentPane().add(btnBack);

		lblErrorMessage = new JLabel("You must select \r\nat least 1 event!");
		lblErrorMessage.setHorizontalAlignment(SwingConstants.CENTER);
		lblErrorMessage.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblErrorMessage.setForeground(new Color(255, 0, 0));
		lblErrorMessage.setBounds(108, 2, 227, 40);
		contentPane.add(lblErrorMessage);

		lblInfo = new JLabel("");
		lblInfo.setHorizontalAlignment(SwingConstants.CENTER);
		lblInfo.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblInfo.setBounds(203, 156, 172, 31);
		contentPane.add(lblInfo);

		// btnDeleteEvent = new JButton("Delete Event");
		// btnDeleteEvent.setFont(new Font("Tahoma", Font.BOLD, 12));
		// btnDeleteEvent.setBounds(240, 105, 135, 40);
		// contentPane.add(btnDeleteEvent);

	}

	/**
	 * For test purpose
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminScreen frame = new AdminScreen(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
